/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.deptBudget.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.db.dao.ProjectDao;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Project;
import com.yunhoo.deptBudget.db.dao.FinBudgetAllotDao;
import com.yunhoo.deptBudget.model.Budget;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.util.common.CommonParam;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

/**
 * 财政预算分配到各个部门
 *
 * @author Administrator
 */
@ManagedBean(name = "budgetAllotBean")
@SessionScoped
public class FinBudgetAllotBean implements Serializable {

    private static final DaoManager daom = DaoConfig.getDaoManager();
    private static final FinBudgetAllotDao dao = (FinBudgetAllotDao) daom.getDao(FinBudgetAllotDao.class);
    private static final ProjectDao projDao = (ProjectDao) daom.getDao(ProjectDao.class);
    //
    private Budget deptproj = new Budget();
    private List<SelectItem> yearli = null;
    private List<Budget> dataList = new ArrayList<Budget>();
    private List<Budget> alldataList = new ArrayList<Budget>();
    private List<Budget> dataTotalList = new ArrayList<Budget>();
    private List<Department> deptlist = null;
    private List<SelectItem> selectItemList;
    private Department dept = new Department();
    private Map<String, String> depts = new HashMap<String, String>();
    private Map<String, String> fina = new HashMap<String, String>();
    private List financial = new ArrayList();
    private Project fin = new Project();
    private List fincodeList;
    private Integer iyear = 0;
    private Integer currentYear = 0;
    private Integer iyearValue = 0;
    private DataModel<Budget> dataModel = new ListDataModel<Budget>();
    private boolean addOrUpdate = false;
    private boolean editAble = true;
    private boolean isallselect;
    private List<Project> finlist = null;
    private String deptcode = "";

    public FinBudgetAllotBean() {
        intiDepartmentInfo();
    }

    public void adddeptProjData() {
        Budget param = new Budget(true);
        param.setProjCode(buildParam());
        param.setDeptCode(deptproj.getDeptCode());
        param.setIyear(currentYear);
        dataList = dao.queryListAllFinProjAndBudget(param);
        //IbatisTemplate.queryForList("getdeptProjList", map);
        dataModel.setWrappedData(dataList);
    }

    public String buildParam() {
        String str = "";
        for (Project f : finlist) {
            if (f.isSelected()) {
                str += "'" + f.getCode() + "'" + ",";
            }
        }
        if (str.length() != 0) {
            str = str.substring(0, str.length() - 1);
        } else {
            str = "''";
        }
        return str;
    }

    public void updatedeptProjData() {
        editAble = false;
    }

    public void deletedeptProjData() {
        List li = (ArrayList) dataModel.getWrappedData();
        li.remove(dataModel.getRowIndex());
        dataModel.setWrappedData(li);
    }

    public void savedeptProjData() {
        Budget param = new Budget(true);
        param.setDeptCode(deptcode);
        param.setProjCode(buildParam());
        try {
            dao.saveList(dataList, param);
        } catch (SQLException ex) {
            Logger.getLogger(FinBudgetAllotBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        editAble = true;
    }

    public void sumEveryTotal() {
        deptproj = (Budget) dataModel.getRowData();
        deptproj.setTotal(deptproj.getBudget() + deptproj.getBudgetOfLastYear());
        System.out.println("total: " + deptproj.getTotal());
    }

    private void intiDepartmentInfo() {
        selectItemList = new ArrayList();
        deptlist = CommonParam.departmentList();
        for (Department d : deptlist) {
            selectItemList.add(new SelectItem(d.getCode(), d.getName()));
            depts.put(d.getCode().trim(), d.getName().trim());
        }
    }

    public void deptChange(ValueChangeEvent event) {
        financial = new ArrayList();
        deptcode = (String) event.getNewValue();
        dept.setCode(deptcode.trim());//设置登陆部门信息
        dept.setName(depts.get(deptcode.trim()));
    }

    public void changeFinProjList() {
        dataModel = new ListDataModel<Budget>();
        financial = new ArrayList();
        deptcode = deptproj.getDeptCode();
        System.out.println("deptcode : " + deptcode);
        if (deptcode == null || deptcode.isEmpty() || currentYear == null || currentYear == 0) {
            return;
        }
        dept.setCode(deptcode.trim());//设置登陆部门信息
        dept.setName(depts.get(deptcode.trim()));
    }

    public void financialChange(ValueChangeEvent event) {
        String finProjCode = (String) event.getNewValue();
        fin.setCode(finProjCode.trim());
        fin.setName(fina.get(finProjCode.trim()));
    }

    public void selectAllDataList() {
        Budget param = new Budget(true);
        param.setIyear(iyear);
        alldataList = dao.queryListAllDeptBudget(param);
    }

    public void selectAll() {
        if (alldataList != null && alldataList.size() > 0) {
            for (int i = 0; i < alldataList.size(); i++) {
                alldataList.get(i).setSelected(isallselect);
            }
        }
    }

    public void saveAllData() {
        List<Budget> datali = new ArrayList();
        for (Budget dep : alldataList) {
            if (dep.isSelected()) {
                datali.add(dep);
            }
        }
        try {
            dao.updateListById(datali);
        } catch (SQLException ex) {
            Logger.getLogger(FinBudgetAllotBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        selectAllDataList();
    }

    public void deleteAllData() {
        List<Budget> tpli = new ArrayList<Budget>();
        for (Budget dep : alldataList) {
            if (dep.isSelected()) {
                tpli.add(dep);
            }
        }
        try {
            dao.deleteListById(tpli);
        } catch (SQLException ex) {
            Logger.getLogger(FinBudgetAllotBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        selectAllDataList();
    }

    public void selectDataTotalList() {
        Double budgetAll = 0d;
        Double lastyeardataAll = 0d;
        Double totalAll = 0d;
        if (dataTotalList != null && dataTotalList.size() > 0) {
            dataTotalList.clear();
        }
        Budget param = new Budget(true);
        param.setIyear(iyearValue);
        List<Budget> templi = dao.queryListCurrentAndLastYearDeptBudget(param);
        if (null == templi) {
            return;
        }
        Budget dh = templi.get(0);
        for (int i = 1; i < templi.size(); i++) {
            if (dh.getDeptCode().equals(templi.get(i).getDeptCode())) {
                templi.get(i).setDeptName("");
            } else {
                dh = templi.get(i);
            }
        }
        Budget total = null;
        String deptCode = "";
        for (int i = 0; i < templi.size(); i++) {
            Budget current = templi.get(i);

            if (!current.getDeptCode().trim().equals(deptCode)) {
                deptCode = templi.get(i).getDeptCode().trim();
                if (i > 0) {
                    dataTotalList.add(total);
                }
                total = new Budget();
                total.setProjName("合计");
            }
            total.setBudget(total.getBudget() + current.getBudget());
            total.setBudgetOfLastYear(total.getBudgetOfLastYear() + current.getBudgetOfLastYear());
            total.setTotal(total.getTotal() + current.getTotal());
            dataTotalList.add(current);
            if (i == templi.size() - 1) {
                dataTotalList.add(total);
            }
        }
        //总计行
        for (Budget p : templi) {
            budgetAll += p.getBudget();
            lastyeardataAll += p.getBudgetOfLastYear();
            totalAll += p.getTotal();
        }
        Budget deptp = new Budget();
        deptp.setDeptName("总计");
        deptp.setBudget(budgetAll);
        deptp.setBudgetOfLastYear(lastyeardataAll);
        deptp.setTotal(totalAll);
        dataTotalList.add(deptp);
    }

    public List<SelectItem> getYearli() {
        if (yearli == null) {
            yearli = CommonParam.yearList();
        }
        return yearli;
    }

    public List<Project> getFinlist() {
        // if (finlist == null) {
        System.out.println("getFinlist !!");
        finlist = projectList();
        // }
        return finlist;
    }

    public List<Project> projectList() {
        List<Project> templi = null;
        try {
            Project param = new Project(true);
            //param.setIyear(iyear);
            templi = projDao.queryAll(param);
        } catch (SQLException ex) {
            Logger.getLogger(CommonParam.class.getName()).log(Level.SEVERE, null, ex);
        }
        return templi;
    }

    public Budget getDeptproj() {
        return deptproj;
    }

    public void setDeptproj(Budget deptproj) {
        this.deptproj = deptproj;
    }

    public List<Budget> getDataList() {
        return dataList;
    }

    public void setDataList(List<Budget> dataList) {
        this.dataList = dataList;
    }

    public List<Budget> getAlldataList() {
        return alldataList;
    }

    public void setAlldataList(List<Budget> alldataList) {
        this.alldataList = alldataList;
    }

    public List<Budget> getDataTotalList() {
        return dataTotalList;
    }

    public void setDataTotalList(List<Budget> dataTotalList) {
        this.dataTotalList = dataTotalList;
    }

    public List<Department> getDeptlist() {
        return deptlist;
    }

    public void setDeptlist(List<Department> deptlist) {
        this.deptlist = deptlist;
    }

    public List<SelectItem> getSelectItemList() {
        return selectItemList;
    }

    public void setSelectItemList(List<SelectItem> selectItemList) {
        this.selectItemList = selectItemList;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }

    public Map<String, String> getDepts() {
        return depts;
    }

    public void setDepts(Map<String, String> depts) {
        this.depts = depts;
    }

    public Map<String, String> getFina() {
        return fina;
    }

    public void setFina(Map<String, String> fina) {
        this.fina = fina;
    }

    public List getFinancial() {
        return financial;
    }

    public void setFinancial(List financial) {
        this.financial = financial;
    }

    public Project getFin() {
        return fin;
    }

    public void setFin(Project fin) {
        this.fin = fin;
    }

    public List getFincodeList() {
        return fincodeList;
    }

    public void setFincodeList(List fincodeList) {
        this.fincodeList = fincodeList;
    }

    public Integer getIyear() {
        return iyear;
    }

    public void setIyear(Integer iyear) {
        this.iyear = iyear;
    }

    public Integer getCurrentYear() {
        return currentYear;
    }

    public void setCurrentYear(Integer currentYear) {
        this.currentYear = currentYear;
    }

    public Integer getIyearValue() {
        return iyearValue;
    }

    public void setIyearValue(Integer iyearValue) {
        this.iyearValue = iyearValue;
    }

    public DataModel<Budget> getDataModel() {
        return dataModel;
    }

    public void setDataModel(DataModel<Budget> dataModel) {
        this.dataModel = dataModel;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditAble() {
        return editAble;
    }

    public void setEditAble(boolean editAble) {
        this.editAble = editAble;
    }

    public boolean isIsallselect() {
        return isallselect;
    }

    public void setIsallselect(boolean isallselect) {
        this.isallselect = isallselect;
    }

    public String getDeptcode() {
        return deptcode;
    }

    public void setDeptcode(String deptcode) {
        this.deptcode = deptcode;
    }
}
